{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 调用数器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一个装饰器，每次调用完之后就自加1\n",
    "\n",
    "def timer(func): #5\n",
    "    cnt=[0]\n",
    "    def deco():  \n",
    "        func()\n",
    "        cnt[0]+=1\n",
    "        return cnt\n",
    "    return deco\n",
    "\n",
    "#test = timer(test) #6\n",
    "\n",
    "@timer#语法省略了 test=timer（test） 的过程  \n",
    "def test():\n",
    "    #time.sleep(1)\n",
    "    print(\"test is running!\")  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test is running!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[4]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [],
   "source": [
    "def creatCounter():  \n",
    "    s = [0]  \n",
    "    def counter():  \n",
    "        s[0] = s[0] + 1  \n",
    "        return s[0]  \n",
    "    return counter\n",
    "c=creatCounter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.creatCounter.<locals>.counter()>"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "creatCounter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "a=c()\n",
    "if a>3:\n",
    "    c=creatCounter() \n",
    "\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "def creatCounter( ):\n",
    "    def f( ):\n",
    "        x = 0\n",
    "        while True:\n",
    "            x = x +1\n",
    "            yield x\n",
    "    \n",
    "    sum = f( ) \n",
    "      \n",
    "    def  counter( ):\n",
    "         return next( sum )\n",
    " \n",
    "    return counter\n",
    "cnt=creatCounter()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnt()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnboundLocalError",
     "evalue": "local variable 'cnt' referenced before assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnboundLocalError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-203-1084c2f93c06>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     20\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"cccc\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     21\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[1;34m\"cccc\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 22\u001b[1;33m \u001b[0mt\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-203-1084c2f93c06>\u001b[0m in \u001b[0;36mfunc_in\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m      6\u001b[0m             \u001b[1;31m# print(\"-----记录日志-----\")\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m             \u001b[1;32mif\u001b[0m \u001b[0mcnt\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0mskip\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m                 \u001b[0mret\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mfunctionName\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m                 \u001b[0mcnt\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mUnboundLocalError\u001b[0m: local variable 'cnt' referenced before assignment"
     ]
    }
   ],
   "source": [
    "def outputCountSieve(skip=0):\n",
    "    cnt = [0]\n",
    "    def func(functionName):\n",
    "\n",
    "        def func_in(*args, **kwargs):\n",
    "            # print(\"-----记录日志-----\")\n",
    "\n",
    "            if cnt[0] > skip:\n",
    "                ret = functionName(*args, **kwargs)\n",
    "                cnt = 0\n",
    "            else:\n",
    "                cnt[0] += 1\n",
    "            return ret\n",
    "\n",
    "        return func_in\n",
    "\n",
    "    return func\n",
    "@outputCountSieve(1)\n",
    "def t():\n",
    "    print(\"cccc\")\n",
    "    return \"cccc\"\n",
    "t()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.numCounter.<locals>.fun.<locals>.counter()>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numCounter()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# ？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from hashlib import *\n",
    "import time\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "77de68daecd823babbb58edb1c8e14d7106e83bb\n"
     ]
    }
   ],
   "source": [
    "print(sha1(b'3').hexdigest())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "def getTimeHash(func):\n",
    "    def inner():\n",
    "        timeval=time.time()\n",
    "        datatime=time.strftime(\"%Y-%m-%d %H:%M:%S\",time.localtime(timeval))\n",
    "        print(sha256(b\"123\").hexdigest())\n",
    "        print( func().__next__())\n",
    "       \n",
    "        return\n",
    "    return inner\n",
    "\n",
    "def createCounter():\n",
    "    def counter():\n",
    "       return next(o)\n",
    "    def f():\n",
    "        i=0\n",
    "        while 1:\n",
    "            i=i+1\n",
    "            \n",
    "            yield i\n",
    "    o=f()\n",
    "    return counter\n",
    "go=createCounter()\n",
    "\n",
    "#print(getTimeHash(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "def tdo_yield():\n",
    "    for i in range(10):\n",
    "        print(i)\n",
    "        yield\n",
    "f=tdo_yield()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "ename": "StopIteration",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m                             Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-aff1dd02a623>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mStopIteration\u001b[0m: "
     ]
    }
   ],
   "source": [
    "next(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "2>1 == True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "\n",
    "def counter():\n",
    "    i=0\n",
    "    while 1:\n",
    "        i+=1\n",
    "        yield i\n",
    "#f=counter()\n",
    "def getTimeHash(gener):\n",
    "     while 1:\n",
    "        timeval=time.time()\n",
    "        datatime=time.strftime(\"%Y-%m-%d %H:%M:%S\",time.localtime(timeval))\n",
    "        hashval=sha256(datatime.encode(\"utf-8\")).hexdigest()\n",
    "        cnt=next(gener)\n",
    "        dic={(datatime+\" \"+str(cnt)):hashval}\n",
    "       # print(dic)\n",
    "        #print(sha256(b\"123\").hexdigest())\n",
    "       # print(\"xxx\",next(f))\n",
    "        yield dic\n",
    "def getdata():\n",
    "    f=counter()\n",
    "    generHash=getTimeHash(f)\n",
    "    for i in generHash:\n",
    "        print(i)\n",
    "        time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2018-07-12 15:06:47': 'b56dea5ce1eb77a597bd1c6d70c796830d4bd78b9128ae5fdf06101511ffa204 1'}\n",
      "{'2018-07-12 15:06:48': '69adc2bd22941dbb9e0c91fee44d7d89d1d37fcd61c917d9703c0927be57292c 2'}\n",
      "{'2018-07-12 15:06:49': '25b9344b1b4e786be5b95ef0ba2fcc33a883edf59faa4a135a87ea6aa436af1a 3'}\n",
      "{'2018-07-12 15:06:50': '779c697f5c27c1c090c8cf48bd40c8de451037f0dad4bdf2f71e86ac8b8bee82 4'}\n",
      "{'2018-07-12 15:06:51': 'e78732ce8062f878f082c5e7b28c65e9949b0cb47ee4a8bf75ab988d7585cb15 5'}\n",
      "{'2018-07-12 15:06:52': 'ff61f15a3a960f053f3af99f6dc8ac1f6f1e82493d20c1db571b1c10921f0b9d 6'}\n",
      "{'2018-07-12 15:06:53': '668111e7729bf7c387ca97a7eaada5f772e9fb48a9a61b11b5c0519b87ec3c53 7'}\n",
      "{'2018-07-12 15:06:54': 'e932638cf3f7cc510a05ed88c8ff3a300979840fa66d118487c5d436a2d66a3d 8'}\n",
      "{'2018-07-12 15:06:55': 'cb800a317cb87b6aafdb378a9f9de5eca4d73960f032786d7fa16868ba71a92b 9'}\n",
      "{'2018-07-12 15:06:56': '0ebc3d79b2ad517136ac788ab048000d98385f4f6a5506dd017624c64ba4ee45 10'}\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-09e7d21b7c1a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#getTimeHash(f)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mgetdata\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-5-c5b243082a36>\u001b[0m in \u001b[0;36mgetdata\u001b[1;34m()\u001b[0m\n\u001b[0;32m     22\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mgenerHash\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 24\u001b[1;33m         \u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "#getTimeHash(f)\n",
    "getdata()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "##这个为什么一直是1 应为没次都建立一个不同的对象\n",
    "#counter().__next__() \n",
    "print(counter().__next__()) #这会一直输出1 且每次对象不同\n",
    "#f=counter()\n",
    "f.__next__()# 自加应为对象是同一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# lambda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# redis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import redis   # 导入redis模块，通过python操作redis 也可以直接在redis主机的服务端操作缓存数据库\n",
    "\n",
    "\n",
    "r = redis.Redis(host='192.168.99.100', port=32768, decode_responses=True)   # host是redis主机，需要redis服务端和客户端都启动 redis默认端口是6379\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "#r.set('name', 'junxi')  # key是\"foo\" value是\"bar\" 将键值对存入redis缓存\n",
    "\n",
    "print(r.get('2018-07-12 16:52:29'))  # 取出键name对应的值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-47-95c2b9e0d46c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mgenerhash\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m     \u001b[0mr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mkeys\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[1;31m#a+=1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\client.py\u001b[0m in \u001b[0;36mset\u001b[1;34m(self, name, value, ex, px, nx, xx)\u001b[0m\n\u001b[0;32m   1169\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mxx\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1170\u001b[0m             \u001b[0mpieces\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'XX'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1171\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute_command\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'SET'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mpieces\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1172\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1173\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__setitem__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\client.py\u001b[0m in \u001b[0;36mexecute_command\u001b[1;34m(self, *args, **options)\u001b[0m\n\u001b[0;32m    666\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    667\u001b[0m             \u001b[0mconnection\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend_command\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 668\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mparse_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconnection\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcommand_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    669\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mConnectionError\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    670\u001b[0m             \u001b[0mconnection\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\client.py\u001b[0m in \u001b[0;36mparse_response\u001b[1;34m(self, connection, command_name, **options)\u001b[0m\n\u001b[0;32m    678\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mparse_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconnection\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcommand_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    679\u001b[0m         \u001b[1;34m\"Parses a response from the Redis server\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 680\u001b[1;33m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconnection\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    681\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mcommand_name\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresponse_callbacks\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    682\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mresponse_callbacks\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mcommand_name\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\connection.py\u001b[0m in \u001b[0;36mread_response\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    622\u001b[0m         \u001b[1;34m\"Read the response from a previously sent command\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    623\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 624\u001b[1;33m             \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_parser\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    625\u001b[0m         \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    626\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\connection.py\u001b[0m in \u001b[0;36mread_response\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    282\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    283\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mread_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 284\u001b[1;33m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_buffer\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    285\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    286\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mConnectionError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mSERVER_CLOSED_CONNECTION_ERROR\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\connection.py\u001b[0m in \u001b[0;36mreadline\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    214\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mendswith\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mSYM_CRLF\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    215\u001b[0m             \u001b[1;31m# there's more data in the socket that we need\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 216\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_read_from_socket\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    217\u001b[0m             \u001b[0mbuf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mseek\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbytes_read\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    218\u001b[0m             \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbuf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreadline\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\connection.py\u001b[0m in \u001b[0;36m_read_from_socket\u001b[1;34m(self, length)\u001b[0m\n\u001b[0;32m    172\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    173\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 174\u001b[1;33m                 \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrecv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_sock\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msocket_read_size\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    175\u001b[0m                 \u001b[1;31m# an empty string indicates the server shutdown the socket\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    176\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbytes\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32me:\\user_sunjun\\environment\\py36\\lib\\site-packages\\redis\\_compat.py\u001b[0m in \u001b[0;36mrecv\u001b[1;34m(sock, *args, **kwargs)\u001b[0m\n\u001b[0;32m     77\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     78\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mrecv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msock\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 79\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrecv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     80\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     81\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mrecv_into\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msock\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "f=counter()\n",
    "generhash=getTimeHash(f)\n",
    "#print(next(generhash))\n",
    "a=0\n",
    "for i in generhash:\n",
    "    r.set(str(i.keys()), str(i.values()))\n",
    "    \n",
    "    #a+=1\n",
    "    #if a>10:break;\n",
    "    #print(i)\n",
    "    #time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=next(generhash)\n",
    "#print(str(data))\n",
    "#print(str(data.keys()))\n",
    "type(str(data.keys()))\n",
    "#print(data.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器 \n",
    "做三件事;1不改变函数内容， 2不改变调用方式，3 满足12添加新功能\n",
    "\n",
    "结构< 函数+实参高阶函数+返回值高阶函数+嵌套函数+语法糖 = 装饰器 >\n",
    "\n",
    "作用？ 在写通用的框架的时候 你可以调用户自己写的函数？ 而约定好的名字不改变？  下面的例子看起来像是 test是被添加的功能 就是说用@timer去装饰test\n",
    "\n",
    "这么说 我在框架内定义好的函数， 用户可以在自己函数中添加这个功能，而且完全不用改变自己的风格 只需要 @框架功能 def myfunc （）  就能够在myfunc添加一个预定的功能\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test is running!\n",
      "0.0\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "def timer(func): #5\n",
    "    def deco():  \n",
    "        start = time.time()\n",
    "        func()\n",
    "        stop = time.time()\n",
    "        print(stop-start)\n",
    "    return deco\n",
    "\n",
    "#test = timer(test) #6\n",
    "\n",
    "@timer#语法省略了 test=timer（test） 的过程  \n",
    "def test():\n",
    "    #time.sleep(1)\n",
    "    print(\"test is running!\")  \n",
    "test() #7\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start\n",
      "test is running!\n",
      "1.0000028610229492\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def timer(func): #5\n",
    "    def deco():  \n",
    "        start = time.time()\n",
    "        print(\"start\")\n",
    "        func()\n",
    "        stop = time.time()\n",
    "        print(stop-start)\n",
    "    return deco\n",
    "def test():\n",
    "    print(\"test is running!\") \n",
    "    time.sleep(1)\n",
    "      \n",
    "test = timer(test) #6\n",
    "test() #\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test is running!\n",
      "1.0009913444519043\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Returned value'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "def timer(func):\n",
    "    def deco(*args, **kwargs):  \n",
    "        start = time.time()\n",
    "        res = func(*args, **kwargs)\n",
    "        stop = time.time()\n",
    "        print(stop-start)\n",
    "        return res \n",
    "    return deco\n",
    "\n",
    "@timer\n",
    "def test(parameter): #8\n",
    "    time.sleep(1)\n",
    "    print(\"test is running!\")   \n",
    "    return \"Returned value\"\n",
    "test(1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=======================\n",
      "=======================\n",
      "in the task1\n",
      "the task1 run time is : 1.0006825923919678\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "# 关于可变参数https://www.cnblogs.com/xuyuanyuan123/p/6674645.html\n",
    "# 选择 带有参数的装饰器\n",
    "def timer(parameter):\n",
    "    print(\"=======================\")\n",
    "    def outer_wrapper(func):\n",
    "\n",
    "        def wrapper(*args, **kwargs):\n",
    "            if parameter == 'task1':\n",
    "                start = time.time()\n",
    "                func(*args, **kwargs)\n",
    "                stop = time.time()\n",
    "                print(\"the task1 run time is :\", stop - start)\n",
    "            elif parameter == 'task2':\n",
    "                start = time.time()\n",
    "                func(*args, **kwargs)\n",
    "                stop = time.time()\n",
    "                print(\"the task2 run time is :\", stop - start)\n",
    "\n",
    "        return wrapper\n",
    "\n",
    "    return outer_wrapper\n",
    "\n",
    "@timer(parameter='task1')\n",
    "def task1(cat ):\n",
    "    time.sleep(1)\n",
    "    print(\"in the task1\")\n",
    "\n",
    "@timer(parameter='task2')\n",
    "def task2():\n",
    "    time.sleep(1)\n",
    "    print(\"in the task2\")\n",
    "\n",
    "task1(\"miao\")\n",
    "#task2()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 通用装饰器 模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----记录日志-----,这里是原函数的空间 1 123\n",
      "test3 value is 123\n",
      "装饰器 让返回值+1 并显示43\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func(functionName):\n",
    "    def func_in(*args, **kwargs):\n",
    "        print(\"-----记录日志-----,这里是原函数的空间\",*args,kwargs[\"a\"])\n",
    "        ret = functionName(*args, **kwargs)\n",
    "        print(\"装饰器 让返回值+1 并显示\"+str(ret+1))\n",
    "        return ret\n",
    "    return func_in\n",
    "\n",
    "@func\n",
    "def test3(c,a=3):\n",
    "    print(\"test3 value is %d\" % a)\n",
    "    return 42\n",
    "    \n",
    "test3(1,a=123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "c=[i for i in range(1)]\n",
    "for i in c:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "装饰函数空间打印出装饰器参数 ('1',) {'p': 1, 'c': 2} p= 1\n",
      "原函数空间，调用原函数 test3\n",
      "装饰函数空间打印出装饰器参数 ('1',) {'p': 2} p= 2\n",
      "原函数空间，调用原函数 test4\n"
     ]
    }
   ],
   "source": [
    "# 带参数的通用装饰器模板\n",
    "def funcp(*args,**kwargs):\n",
    "    def func(functionName):\n",
    "        print(\"原函数空间，调用原函数\",functionName.__name__)\n",
    "        def func_in(*args, **kwargs):\n",
    "            print(\"-----记录日志-----\")\n",
    "            ret = functionName(*args, **kwargs)\n",
    "            print(\"装饰器 让返回值+1 并显示\"+str(ret+1),\"这里func_in只能显示原函数的参数\",func_in.__name__,args)\n",
    "            return ret\n",
    "        return func_in\n",
    "    print(\"装饰函数空间打印出装饰器参数\",args,kwargs,\"p=\",kwargs[\"p\"])\n",
    "    return func\n",
    "\n",
    "@funcp(\"1\",p=1,c=2)\n",
    "def test3(a):\n",
    "    print(\"test3 value is %d\" % a)\n",
    "    return 42\n",
    "\n",
    "@funcp(\"1\",**{\"p\":2})\n",
    "def test4(a):\n",
    "    print(\"test3 value is %d\" % a) \n",
    "    return 42\n",
    "#test3(123)\n",
    "#test4(223)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 多个装饰器从上到下调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "装饰函数空间打印出装饰器参数 () {'p': 1} p= 1\n",
      "原函数空间，调用原函数 test5\n",
      "原函数空间，调用原函数 func_in\n",
      "-----记录日志-----\n",
      "-----记录日志-----\n",
      "装饰器 让返回值+1 并显示23\n",
      "装饰器 让返回值+1 并显示23 这里func_in只能显示原函数的参数 func_in ()\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def funcp(*args,**kwargs):\n",
    "    def func(functionName):\n",
    "        print(\"原函数空间，调用原函数\",functionName.__name__)\n",
    "        def func_in(*args, **kwargs):\n",
    "            print(\"-----记录日志-----\")\n",
    "            ret = functionName(*args, **kwargs)\n",
    "            print(\"装饰器 让返回值+1 并显示\"+str(ret+1),\"这里func_in只能显示原函数的参数\",func_in.__name__,args)\n",
    "            return ret\n",
    "        return func_in\n",
    "    print(\"装饰函数空间打印出装饰器参数\",args,kwargs,\"p=\",kwargs[\"p\"])\n",
    "    return func\n",
    "\n",
    "def func(functionName):\n",
    "    print(\"原函数空间，调用原函数\",functionName.__name__)\n",
    "    def func_in(*args, **kwargs):\n",
    "        print(\"-----记录日志-----\")\n",
    "       \n",
    "        ret = functionName(*args, **kwargs)\n",
    "        print(\"装饰器 让返回值+1 并显示\"+str(ret+1))\n",
    "        return ret\n",
    "    return func_in\n",
    "\n",
    "@funcp(p=1)\n",
    "@func\n",
    "def  test5():\n",
    "    return 22\n",
    "test5()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 闭包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n",
      "44\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "def make_adder(a):\n",
    "    print(a)\n",
    "    def adder(b):\n",
    "        return a + b\n",
    "    return adder\n",
    "\n",
    "p = make_adder(23)\n",
    "q = make_adder(44)\n",
    "\n",
    "print( p(100))\n",
    "#print (q(100))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fun 101\n",
      "101\n"
     ]
    }
   ],
   "source": [
    "# 内部函数\n",
    "def make_adder(a):\n",
    "    def adder():\n",
    "        return a + 100\n",
    "    fun=adder\n",
    "    print(\"fun\",fun())\n",
    "    return adder()\n",
    "print ( make_adder(1))\n",
    "#print ( make_adder(100))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
